<?php

/**
 * Pes for PHP 5.3+
 *
 * Copyright (c) 2013 phpeskill.com (http://www.phperskill.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace Model\Admin;

/**
 * 节点模型
 * @author LazyPHP
 * @version 1.0
 */
class NodeModel extends CommonModel {

    private static $li = 1, $selected;
    
    public static $tree = '';

    /**
     * 获取节点信息
     * @param type $value 查找的值
     * @param type $type 查找的类型|默认为ID
     * @return boolean 提交参数为空时，返回二维数组。参数不为空则返回一维数组。没有结果则返回false;
     */
    public static function getNode($value = "", $type = "id") {
        return self::GetTableContent($value, $type, 'node');
    }

    /**
     * 排序节点
     * 代码较旧，以后有机会在调整了，目前满足使用。
     * @param type $array 节点原数组
     * @param type $array_2 节点原数组
     * @param type $id 节点ID
     * @return type
     */
    public static function getListNode($array, $array_2, $id = '0') {
        $count = count($array_2);
        for ($i = 0; $i < $count; $i++) {
            if ($id == 0 && $array[$i]['pid'] == 0) {
                //获取第一节点
                $node['competence'][$array[$i]['name']] = array('title' => $array[$i]['title'], 'id' => $array[$i]['id'], 'imit' => array());
            }

            foreach ($array as $key => $value) {
                //获取第二节点
                if ($array[$i]['id'] == $value['pid'] && $array[$i]['pid'] == 0) {
                    $temp[$value['name']] = array('title' => $value['title'], 'id' => $value['id'], 'imit' => array());

                    foreach ($array as $key_2 => $value_2) {
                        //获取第三节点
                        if ($value['id'] == $value_2['pid']) {
                            $temp[$value['name']]['imit'][] = array('title' => $value_2['title'], 'id' => $value_2['id']);
                        }
                        $child = array_merge($node['competence'][$array[$i]['name']]['imit'], $temp);
                        $node['competence'][$array[$i]['name']]['imit'] = $child;
                    }
                    //清空处理节点数据
                    unset($temp, $child);
                }
            }
        }

        return $node;
    }

    /**
     * 输出节点表单
     * @return type
     */
    public static function getSelectNode($value = '') {
        self::$selected = $value;
        $list = self::getNode();
        self::setInputOption($list, $list);
        return self::$tree;
    }

    /**
     * 用于option表单输出
     * @param $array 数组一
     * @param $array_2 数组二
     * @param $id 父类产品ID
     * @param $depth 用于判断递归深度
     */
    private static function setInputOption($array, $array_2, $id = '0', $depth = '0') {
        $count = count($array_2);
        if ($depth < 3) {
            for ($i = 0; $i < $count; $i++) {
                if ($id == 0 && $array[$i]['pid'] == 0) {
                    $selected = self::$selected == $array[$i]['id'] ? 'selected="selected"' : '';
                    self::$tree .= '<option value="' . $array[$i]['id'] . '"' . $selected . '>' . $array[$i]['title'] . '</option>';
                }
                foreach ($array as $key => $value) {
                    self::$li = 1;
                    if ($array[$i]['id'] == $value['pid'] && $array[$i]['pid'] == 0) {
                        $selected = self::$selected == $value['id'] ? 'selected="selected"' : '';
                        self::$tree .= '<option value="' . $value['id'] . '"' . $selected . '>' . self::nbsp(self::$li) . '└─' . $value['title'] . '</option>';
                        self::setInputOption($array[$i], $array_2, $value['id'], 3);
                    }
                }
            }
        }
    }

    /**
     * 列出所有子类的ID(含父类)
     * @param type $id 父类的ID
     */
    public static function listChildId($id) {
        self::$tree = $id;
        self::findChildId($id);
        return self::$tree;
    }

    /**
     * 查找符合的子分类
     * @param $array 数组一
     * @param $array_2 数组二
     * @param $id 父类产品ID
     * @param $depth 用于判断递归深度
     */
    private static function findChildId($id) {
        $db = self::db('node');
        $result = $db->where('pid = :pid')->select(array('pid' => $id));
        if (!empty($result)) {
            foreach ($result as $key => $value) {
                self::$tree .= ',' . $value['id'];
                self::findChildId($value['id']);
            }
        }
    }

}